#include "zglFont.h"
#include "zglEngine.h"

#pragma warning (push)
#pragma warning (disable : 4996)

#include "sStrings.h"

namespace sgui
{
namespace engine
{

void zglFont::draw(const engine::sDrawContext& context, const std::wstring& text, Color color) const 
{
	if (m_font)
	{
		std::string _text(wstring_to_string(text));

		text_DrawEx(m_font,
			0, 0, 1, 0, _text.c_str(), color >> 24, color & 0xFFFFFF);
	}
}
	
void zglFont::update(const preset::sPreset& preset)
{
	//
}

math::sPointf zglFont::getSize(const std::wstring& text) const
{
	if (m_font)
	{
		char *textData = (char*)alloca(text.size() + 1);
		wstring_to_string(text, textData);

		const float _w = text_GetWidth(m_font, textData, 0);
		const float _h = text_GetHeight(m_font, _w + 2, textData, 1, 0);

		return math::sPointf(_w, _h);
	}
	return math::sPointf(0, 0);
}
	
zglFont::zglFont(zglEngine& engine, const res::sFontResource& res, const preset::sFontPreset& preset) :
	sFont(engine, res, preset), m_engine(engine), m_font(NULL), m_preset(preset)
{
	if (!res.getContents().empty())
	{
		const sgui::res::sResourceContent* cnt = res.getContents()[0];
		if (cnt)
		{
			zglTMemory mem;

			mem.Memory = cnt->getData();
			mem.Position = 0;
			mem.Size = cnt->getDataSize();

			m_font = font_LoadFromMemory(mem);

			if (m_font)
			{
				for (int i = 0; i < m_font->Count.Pages; ++i)
				{
					const sgui::res::sResourceContent* cnt_t = res.getContents()[i + 1];

					mem.Memory = cnt_t->getData();
					mem.Position = 0;
					mem.Size = cnt_t->getDataSize();

					std::string ext = wstring_to_string(cnt_t->getExtention());

					m_font->Pages[i] = tex_LoadFromMemory(mem, ext.c_str());
				}
			}
		}
	}
}

};
};

#pragma warning (pop)